<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="440px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        style="margin-left: 55px"
      >
        <el-col :span="24">
          <el-form-item label="机种代码" prop="jzmJzdm">
            <el-input
              v-model="queryParams.jzmJzdm"
              placeholder="请输入机种代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户名称" prop="jzmCusId">
            <el-select v-model="queryParams.jzmCusId" placeholder="请选择客户">
              <el-option
                v-for="dict in customerOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:jzmMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:jzmMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:jzmMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:jzmMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="jzmMstrList"
      border
      stripe
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机种代码" width="100" prop="jzmJzdm" />
      <el-table-column
        label="客户名称"
        width="190"
        show-overflow-tooltip
        prop="jzmCusId"
      >
        <template #default="scope">
          <dict-tag :options="customerOptions" :value="scope.row.jzmCusId" />
        </template>
      </el-table-column>
      <el-table-column label="有效标识" align="center" width="80" key="jzmFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.jzmFlag"
            active-value="0"
            inactive-value="1"
            @change="changejzmFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        width="80"
        show-overflow-tooltip
        align="center"
        prop="jzmDesc"
      />
      <el-table-column
        label="建立人员"
        width="80"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        align="center"
        prop="createTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更改人员"
        align="center"
        width="80"
        prop="updateBy"
      />
      <el-table-column
        label="更改日期"
        align="center"
        prop="updateTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:jzmMstr:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:jzmMstr:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改模具机种信息对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机种代码" prop="jzmJzdm">
          <el-input v-model="form.jzmJzdm" placeholder="请输入机种代码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="jzmCusId">
          <el-select v-model="form.jzmCusId" placeholder="请选择客户">
            <el-option
              v-for="dict in customerOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效标识">
          <el-radio-group v-model="form.jzmFlag">
            <el-radio
              v-for="dict in jzmFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="jzmDesc">
          <el-input v-model="form.jzmDesc" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJzmMstr,
  getJzmMstr,
  delJzmMstr,
  addJzmMstr,
  updateJzmMstr,
  exportJzmMstr,
  changeJtmMstrFlag,
} from "@/api/project/jzmMstr";
import { getAllCustomer } from "@/api/project/customer";

export default {
  name: "JzmMstr",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      jzmJzdms: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模具机种信息表格数据
      jzmMstrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 有效标识字典
      jzmFlagOptions: [],
      customerOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        jzmJzdm: null,
        jzmCusId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jzmJzdm: [
          { required: true, message: "机种代码不能为空", trigger: "blur" },
        ],
        jzmCusId: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_flag").then((response) => {
      this.jzmFlagOptions = response.data;
    });
    getAllCustomer().then((response) => {
      response.data.map((i) => {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      });
      this.customerOptions = response.data;
    });
  },
  methods: {
    /** 查询模具机种信息列表 */
    getList() {
      this.loading = true;
      const tableH = (document.body.clientHeight - 160) / 2;
      this.queryParams.pageSize = tableH > 350 ? 25 : 15;
      listJzmMstr(this.queryParams).then((response) => {
        this.jzmMstrList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        jzmId: null,
        jzmJzdm: null,
        jzmName: null,
        jzmFlag: "0",
        jzmDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        jzmCusId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jzmId);
      this.jzmJzdms = selection.map((item) => item.jzmJzdm);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模具机种信息";
    },
    handleSearch() {
      this.reset();
      this.title = "查询机台特性";
      this.openSearch = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jzmId = row.jzmId || this.ids;
      getJzmMstr(jzmId).then((response) => {
        this.form = response.data;
        this.form.jzmCusId = this.form.jzmCusId + "";
        this.open = true;
        this.title = "修改模具机种信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.jzmId != null) {
            updateJzmMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJzmMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jzmIds = row.jzmId || this.ids;
      const jzmJzdmss = row.jzmJzdm || this.jzmJzdms;
      this.$confirm(
        '是否确认删除机种代码为"' + jzmJzdmss + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delJzmMstr(jzmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    changejzmFlag(row) {
      let text = row.jzmFlag === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changeJtmMstrFlag(row.jzmId, row.jzmFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.jzmFlag = row.jzmFlag === "0" ? "1" : "0";
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有模具机种信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportJzmMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
